﻿using NCS.IConnect.Core.Utilities;
using NCS.IConnect.Core.WebApi.Description;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.ModelBinding;

namespace NCS.IConnect.Core.WebApi
{
    /// <summary>
    /// An <see cref="ApiController"/> to get error information.
    /// </summary>
    [RoutePrefix("errors")]
    [SuppressDocumentationExport]
    [ComVisible(false)]
    public class ErrorsController : ApiControllerBase
    {
        /// <summary>
        /// Gets the <see cref="ErrorInfo"/> based on specified error code.
        /// </summary>
        /// <param name="errorCode">The error code.</param>
        /// <returns>IHttpActionResult.</returns>
        [Route("{errorCode}")]
        public IHttpActionResult Get(string errorCode)
        {
            ErrorInfo error;
            if (!Errors.TryGetError(errorCode, out error))
            {
                return this.NotFound();
            }
            error.Uri = this.GenerateUri(errorCode);
            return this.Content(HttpStatusCode.OK, error);
        }

        /// <summary>
        /// Gets the route prefix.
        /// </summary>
        /// <value>The route prefix.</value>
        public override string RoutePrefix
        {
            get { return "errors";  }
        }

        private string GenerateUri(string errorCode)
        {
            return string.Format("{0}/{1}/{2}", this.GetBaseUrl(), this.RoutePrefix, errorCode).ToLower();
        }
    }
}